package com.daihou.BCJ.sysconfig;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.daihou.config.TheAuth;
import com.daihou.model.ceres.SysConfigUrlEnum;
import com.daihou.utils.DHConfigFile;
import com.daihou.utils.DHHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class SmsTemplate {
    private static final Logger logger = LoggerFactory.getLogger(SmsTemplate.class);
    private String url;
    Map<String, String> headers = new HashMap<>();
    private int smsTemplateId;

    @BeforeTest(description = "测试准备工作,获取Authorization对象")
    public void beforeTest() throws IOException {
        url = DHConfigFile.getUrl("DaiHou_URL", "systemconfig", SysConfigUrlEnum.TEMPLATEADD.toString());
        //设置请求头中的Authorization
        headers.put("Authorization", TheAuth.getAuth());
        headers.put("X-UserName", "admin");
    }

    @Test(description = "短信模板增加")
    public void smsAddTest() {
        String body = "{\"id\":null,\"code\":\"test\",\"name\":\"测试模板\",\"productType\":\"HAOHUAN\",\"contactType\":\"SELF\",\"overDueDaysStart\":10,\"overDueDaysEnd\":null,\"content\":\"测试\",\"sysType\":\"Manual\",\"limit\":true,\"collOrg\":[\"root\",\"WWGLZ\",\"WWGSA\",\"5WWS1A\"]}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("短信模板增加结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "短信模板查询", dependsOnMethods = "smsAddTest")
    public void smsQueryTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "systemconfig", SysConfigUrlEnum.TEMPLATEQUERY.toString());
        JSONObject body = new JSONObject();
        body.put("pageNum", 1);
        body.put("pageSize", 20);
        body.put("code", "test");
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("短信模板查询查询结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
        smsTemplateId = JSON.parseObject(result).getJSONObject("data").getJSONArray("list").getJSONObject(0).getIntValue("id");
    }

    @Test(description = "短信模板编辑", dependsOnMethods = "smsQueryTest")
    public void smsEditTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "systemconfig", SysConfigUrlEnum.TEMPLATEEDIT.toString());
        String body = "{\"id\":" + smsTemplateId + ",\"code\":\"test\",\"name\":\"测试模板\",\"productType\":\"HAOHUAN\",\"contactType\":\"SELF\",\"overDueDaysStart\":10,\"overDueDaysEnd\":null,\"content\":\"测试d\",\"sysType\":\"Manual\",\"limit\":true,\"collOrg\":[\"root\",\"WWGLZ\",\"WWGSA\",\"5WWS1A\",\"3WWM2A\"]}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("短信模板增加结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "短信模板删除", dependsOnMethods = "smsEditTest")
    public void smsDelTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "systemconfig", SysConfigUrlEnum.TEMPLATEDELETE.toString());
        JSONObject body = new JSONObject();
        body.put("id", smsTemplateId);
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body.toJSONString(), headers)).toJSONString();
        logger.info("短信模板删除结果:{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }
}